import {defineStore} from 'pinia';

interface ObjectList {
    [key: string]: string[];
}

export const usePermissStore = defineStore('permiss', {
    state: () => {
        const keys = localStorage.getItem('ms_keys');
        return {
            key: keys ? JSON.parse(keys) : <string[]>[],
            defaultList: <ObjectList>{
                admin: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16'],
                user: ['1', '2', '3', '11', '13', '14', '15']
            },
            _userRole: 'user'
        };
    },
    getters: {
        userRole: state => {
            return state._userRole;
        }
    },
    actions: {
        handleSet(val: string[]) {
            this.key = val;
        },
        setUserRole(val: string) {
            if (val === '超级管理员') {
                val = 'superAdmin'
            } else if (val.indexOf('管理员') > -1) {
                val = 'admin'
            } else {
                val = 'user'
            }
            this._userRole = val;
        }
    },
    persist: true,
});
